<?php
namespace App\Common\Traits;

use Illuminate\Http\JsonResponse;

trait ResponseTrait
{
    //
    public function success($data,$msg='操作成功'):JsonResponse{
        $jsonData=[
            'status'=>'success',
            'data'=>$data,
            'msg'=>$msg,
        ];
        return response()->json($jsonData);
    }

    //
    public function successMsg($msg='操作成功'):JsonResponse{
        $jsonData=[
            'status'=>'success',
            'msg'=>$msg,
        ];
        return response()->json($jsonData);
    }

    //
    public function fail($error='操作失败',$code=201):JsonResponse{
        $jsonData=[
            'status'=>'fail',
            'error'=>$error,
        ];
        return response()->json($jsonData,$code);
    }
    //
    public function unauthorized($error='未登录或登录已过期',$code=401):JsonResponse{
        $jsonData=[
            'status'=>'unauthorized',
            'error'=>$error,
        ];
        return response()->json($jsonData,$code);
    }

}
